﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class HanhKhachDAO 
    {
        public HanhKhachDTO GetChitiet(string i)
        {

            String sql = "SELECT *";
            sql += "from HanhKhach ";
            sql += " where MaHanhKhach = '" + i + "'";
            SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            HanhKhachDTO hk = new HanhKhachDTO();
            if (reader!=null)
            {

                while (reader.Read())
                {

                        hk.MaHK = reader.GetString(0);
                        hk.TenHK = reader.GetString(1);
                        hk.CMND1 = reader.GetString(2);
                        hk.DienThoai = reader.GetString(3);

                }
            }
            conn.Close();
            return hk;

        }
        #region Đinh Vũ Phương
        // Hàm thực thi cập nhật: Thêm, Xóa, Sửa.
        public int CapNhat(HanhKhachDTO hk, string sql)
        {
            SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
            SqlCommand cmd = new SqlCommand(sql, conn);
            int res = -1;
            res = cmd.ExecuteNonQuery();
            conn.Close();
            return res;
        }
        //Lấy danh sách khách hàng
        public List<HanhKhachDTO> getAll()
        {
            String sql = "SELECT * ";
            sql += "from HanhKhach";
            SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            List<HanhKhachDTO> ds = new List<HanhKhachDTO>();
            while (reader.Read())
            {
                HanhKhachDTO hk = new HanhKhachDTO();
                hk.MaHK = reader.GetString(0);
                hk.TenHK = reader.GetString(1);
                hk.CMND1 = reader.GetString(2);
                hk.DienThoai = reader.GetString(3);
                ds.Add(hk);

            }
            conn.Close();
            return ds;
        }
        //Kiểm tra tồn tại mã khách hàng trong bảng khách hàng. Nếu có trả về true ngược lại trả về false.
        public bool KTMaHK(string MaHK)
        {
            string sql = "select * from HanhKhach where MaHanhKhach='" + MaHK + "'";
            SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                return true;                
            }
            conn.Close();
            return false;            
        }
        #endregion
    }
}
